package com.gaojiawei.multiThread06_ThreeSpecialVisibility;

import org.junit.Test;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 使用lock来保证线程的可见性
 */
public class l04_ResolveVisibilityWithLock {

    private boolean flag = true;

    private ReentrantLock lock = new ReentrantLock();


    @Test
    public void testLock() throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (flag) {
                lock.lock();
                try {

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
            System.out.println("Thread is stopped");
        });

        thread.start();

        Thread.sleep(100);

        flag = false;
    }

}
